package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import top.deepdesigner.mapper.DtsGrouponRulesMapper;
import top.deepdesigner.pojo.DtsGroupon;
import top.deepdesigner.pojo.DtsGrouponRules;
import top.deepdesigner.service.DtsGrouponService;
import top.deepdesigner.vo.GroupOnRecordVO;
import top.deepdesigner.vo.SubGroupRecordVO;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/23 星期五 22:31:09 <br/>
 */
@Service
public class DtsGrouponServiceImpl extends ServiceImpl<BaseMapper<DtsGroupon>, DtsGroupon> implements DtsGrouponService {

  @Autowired
  private DtsGrouponRulesMapper dtsGrouponRulesMapper;

  @Override
  @Transactional
  public List<GroupOnRecordVO> getCouponRecordList(Integer page, Integer limit, String sort, String order, Integer goodsId) {
    //通过商品Id搜索团购规则id
    List<Integer> roleIds = dtsGrouponRulesMapper.selectList(new LambdaQueryWrapper<DtsGrouponRules>()
            .eq(DtsGrouponRules::getGoodsId, goodsId))
        .stream()
        .map(DtsGrouponRules::getId)
        .collect(Collectors.toList());

    QueryWrapper<DtsGroupon> wrapper = new QueryWrapper<>();
    wrapper.eq(true, "deleted", 0)
        .in(roleIds.size() > 0, "rules_id", roleIds)
        .orderBy(!StringUtils.isEmpty(sort), "asc".equalsIgnoreCase(order), sort);
    //根据分页获取
    List<DtsGroupon> dtsGroupon = this.page(new Page<>(page, limit), wrapper).getRecords();

    ArrayList<GroupOnRecordVO> groupOnRecordVOS = new ArrayList<>();

    for (DtsGroupon groupon : dtsGroupon) {
      DtsGrouponRules grouponRules = dtsGrouponRulesMapper.selectById(groupon.getRulesId());

      GroupOnRecordVO groupOnRecordVO = new GroupOnRecordVO();
      groupOnRecordVO.setGroupon(groupon);

      ArrayList<SubGroupRecordVO> groupRecordVOS = new ArrayList<>();
      SubGroupRecordVO subGroup = new SubGroupRecordVO();
      subGroup.setUserId(groupon.getUserId());
      subGroup.setOrderId(groupon.getOrderId());
      subGroup.setLength(grouponRules.getDiscountMember().longValue());
      groupRecordVOS.add(subGroup);
      groupOnRecordVO.setSubGroupons(groupRecordVOS);
      groupOnRecordVO.setGroupon(groupon);

      groupOnRecordVO.setRules(grouponRules);

      groupOnRecordVOS.add(groupOnRecordVO);
    }
    return groupOnRecordVOS;
  }

  @Override
  public Long getCouponRecordCount() {
    return this.count(new LambdaQueryWrapper<DtsGroupon>().eq(DtsGroupon::getDeleted, 0));
  }
}
